package lazy;

import java.util.*;

/**
 * @Description
 * @Author zhousl
 * @Date 2023/4/3 22:42
 */
public class Main {
    public static void main(String[] args){
        //定义字符串 a=This is an apple
        String a = null;
        //转换成数组
        Scanner in = new Scanner(System.in);
        if(in.hasNextLine()){
            a = in.nextLine();
        }
        if(a==null){
            System.out.println("请重新输入！");
        }
        String[] a1 = a.split(" ");
        Map<String,Integer> map = new HashMap<>(16);
        //遍历统计每个单单词出现的次数
        for(String var:a1){
            var = getS(var);
            if(map.get(var)==null){
                map.put(var,1);
            }else {
                map.put(var,map.get(var)+1);
            }
        }

        Map<Integer, List<String>> list = new HashMap<>();
        map.forEach((key,value) ->{
            if(list.get(value)==null){
                List<String> stringList = new ArrayList<>();
                stringList.add(key);
                list.put(value,stringList);
            }else {
                list.get(value).add(key);
            }
        });

        StringBuffer stringBuffer = new StringBuffer();
        Set<Integer> integerSet = list.keySet();
        ArrayList<Integer> integerArrayList = new ArrayList<>(integerSet);
        integerArrayList.sort((s1,s2)->{
            return s2-s1;
        });

        for(Integer set :integerArrayList){
            List<String> strings = list.get(set);
            for(int i=0;i<strings.size();i++){
                for(int j=i+1; j<strings.size();j++){
                    if(strings.get(i).length()>strings.get(j).length()){
                        String temp = strings.get(i);
                        strings.set(i,strings.get(j));
                        strings.set(j,temp);
                    }

                    if(strings.get(i).length() == strings.get(j).length()){
                        for(int b=0;b<strings.get(i).length();b++){
                            if(strings.get(i).charAt(b)!=strings.get(j).charAt(b) && strings.get(i).charAt(b) > strings.get(j).charAt(b)){
                                String t = strings.get(i);
                                strings.set(i,strings.get(j));
                                strings.set(j,t);
                                break;
                            }
                        }
                    }
                }
            }
            for(int m=0;m<strings.size();m++){
                for(Integer integer=0;integer<set;integer++){
                    stringBuffer.append(strings.get(m)+" ");
                }
            }
        }
        System.out.println(stringBuffer.subSequence(0,stringBuffer.length()-1));
    }

    private static String getS(String var){
        char[] chars = var.toCharArray();
        for(int i=0;i<var.length();i++){
            for(int j=i+1;j<var.length();j++){
                if(chars[i]>chars[j]){
                    char temp = chars[i];
                    chars[i] = chars[j];
                    chars[j] = temp;
                }
            }
        }
        return String.valueOf(chars);
    }
}
